package com.liuweiheng.cloud.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import com.liuweiheng.base.entity.R;
import com.liuweiheng.base.utils.GlobalRedisUtil;
import com.liuweiheng.cloud.entity.PostParam;
import com.liuweiheng.cloud.entity.SfUserInfo;
import io.micrometer.core.instrument.util.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 测试配置中心不同dataid是否能获取
 * @author lwh
 * @since 2021/1/20
 */
@RestController
@RequestMapping("/base/")
@NacosPropertySource(dataId = "base.properties", autoRefreshed = true)
@Api(value = "基础控制器")
public class BaseContoller {

    @Autowired
    private RestTemplate restTemplate;

    @NacosValue(value = "${nacos.hello}", autoRefreshed = true)
    private String hello;

    @ApiOperation(value = "测试方法")
    @GetMapping("test")
    public R test() {
        return R.ok(String.valueOf(GlobalRedisUtil.setVal("age", "100")));
    }

    @GetMapping("link")
    public R testLink() {
        PostParam postParam = PostParam.builder().appKey("9122ACB1-E73E-4B13-8424-6392CEF02085")
                                                 .timestamp("1")
                                                 .sysType("AD||")
                                                 .sign("b4bec275a9e749b69036c7988bcf13cb").build();

        Map<String, String> param = new HashMap<>();
        param.put("appToken", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJOYW1lIjoi5rWL6K-V5LiT55SoIiwibmJmIjoxNjIzODk4MTg1LCJleHAiOjE2MjM4OTk5ODV9.kYLrkdkDwfQIbMd2a8eAH7LmjBi4EfICfb-AvapGq8Y");
        param.put("appKey", "9122ACB1-E73E-4B13-8424-6392CEF02085");
        param.put("orgId", "DEB5D187-9D67-4486-88A7-306814FCDAD4");

        StringBuilder paramStr = new StringBuilder();

        for (String key : param.keySet()) {
            paramStr.append(key).append("=").append(param.get(key)).append("&");
        }
        paramStr.delete(paramStr.length() - 1, paramStr.length());

        String url = "http://localhost:7420/api/OpenApi/IndexPost?" + paramStr.toString();

        for (int i = 0; i < 10; i++) {
            try {
                ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, postParam, String.class);
                String body = responseEntity.getBody();
                R r = JSONUtil.toBean(JSONUtil.parseObj(body), R.class);
                System.out.println(r);
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return R.ok();
    }


    @GetMapping("link2")
    public R testLink2() {
        PostParam postParam = PostParam.builder().appKey("9122ACB1-E73E-4B13-8424-6392CEF02085")
                .timestamp("1")
                .sysType("AD||")
                .sign("b4bec275a9e749b69036c7988bcf13cb").build();

        Map<String, String> param = new HashMap<>();
        param.put("appToken", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJOYW1lIjoi5rWL6K-V5LiT55SoIiwibmJmIjoxNjIzODk4MTg1LCJleHAiOjE2MjM4OTk5ODV9.kYLrkdkDwfQIbMd2a8eAH7LmjBi4EfICfb-AvapGq8Y");
        param.put("appKey", "9122ACB1-E73E-4B13-8424-6392CEF02085");
        param.put("orgId", "DEB5D187-9D67-4486-88A7-306814FCDAD4");

        StringBuilder paramStr = new StringBuilder();

        for (String key : param.keySet()) {
            paramStr.append(key).append("=").append(param.get(key)).append("&");
        }
        paramStr.delete(paramStr.length() - 1, paramStr.length());

        String url = "http://localhost:7420/api/OpenApi/IndexPost?" + paramStr.toString();

        for (int i = 0; i < 10; i++) {
            try {
                ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, postParam, String.class);
                String body = responseEntity.getBody();
                R r = JSONUtil.toBean(JSONUtil.parseObj(body), R.class);
                System.out.println(r);
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return R.ok();
    }

    @GetMapping("SF")
    public R testSF() {

        List<SfUserInfo> userInfo = new ArrayList<>();
        userInfo.add(SfUserInfo.builder()
                .account("杨倩30")
                .serverIp("127.0.0.1")
                .serverType("windows")
                .endDate("2021/09/02").build());
        userInfo.add(SfUserInfo.builder()
                .account("gem")
                .serverIp("39.106.59.23")
                .serverType("linux")
                .endDate("2021/09/02").build());

        PostParam param = PostParam.builder().userInfo(userInfo).build();

        String url = "http://localhost:54034/api/Admin/AddTemporaryPermissionBatch";

        try {
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, param, String.class);
            String body = responseEntity.getBody();
            R r = JSONUtil.toBean(JSONUtil.parseObj(body), R.class);
            System.out.println(r);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return R.ok();
    }

}
